package org.geektime.task.automybatis.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 运行需要的配置信息
 */
@Configuration
public class MyBatisConfiguration {

    @Autowired
    private DataSourceProperties dataSourceProperties;

    @Bean
    public DataSource dataSource(){
        //定义C3P0的数据源
        ComboPooledDataSource dataSource = new ComboPooledDataSource() ;
        try{
            dataSource.setDriverClass(dataSourceProperties.getDriverClass());
            dataSource.setJdbcUrl(dataSourceProperties.getJdbcUrl());
            dataSource.setUser(dataSourceProperties.getUsername());
            dataSource.setPassword(dataSourceProperties.getPassword());
            dataSource.setMinPoolSize(5);
            dataSource.setMaxPoolSize(20);
            dataSource.setInitialPoolSize(2);
            dataSource.setBreakAfterAcquireFailure(false);
            dataSource.setAcquireIncrement(5);
        }catch (Exception e){
            e.printStackTrace();
        }
        return dataSource;
    }







}
